package com.yyyyglxt.yygl.recordManage.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.yyyyglxt.common.controller.BaseController;
import com.yyyyglxt.yygl.workdayManage.domain.WorkdayDO;
import com.yyyyglxt.yygl.workdayManage.service.WorkdayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yyyyglxt.yygl.recordManage.domain.RecordDO;
import com.yyyyglxt.yygl.recordManage.service.RecordService;
import com.yyyyglxt.common.utils.PageUtils;
import com.yyyyglxt.common.utils.Query;
import com.yyyyglxt.common.utils.R;

/**
 * 患者预约记录表
 * 
 * @author qichao
 * @date 2022-04-30 17:10:55
 */
 
@Controller
@RequestMapping("/yygl/record")
public class RecordController extends BaseController {
	@Autowired
	private RecordService recordService;

	@Autowired
	private WorkdayService workdayService;
	
	@GetMapping()
	String Record(){
	    return "system/record/record";
	}

	@GetMapping("/listByDoc")
	String listByDoc(Model model){
		model.addAttribute("did",getUserId());
		return "yygl/record/listByDoc";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<RecordDO> recordList = recordService.list(query);
		int total = recordService.count(query);
		PageUtils pageUtils = new PageUtils(recordList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(@RequestParam Map<String, Object> params,Model model){
		model.addAttribute("pid",getUserId());
		model.addAttribute("wid",params.get("wid"));
		WorkdayDO workdayDO = workdayService.get(Integer.valueOf(params.get("wid").toString()));
		model.addAttribute("did",workdayDO.getDid());
		model.addAttribute("visitdate",params.get("visitdate"));
		model.addAttribute("visitnoon",workdayDO.getAmpm());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		model.addAttribute("ordertime",sdf.format(new Date()));
		model.addAttribute("state","成功");

//		model.addAttribute("nsnum",workdayDO.getNsnum());

		int[] nsNums =new int[workdayDO.getNsnum()];//号源池 0有 1无
		List<RecordDO> recordList = recordService.list(params);
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < nsNums.length; i++) {
			int serialnumber = i;//就诊序号从零开始
			//判断当前号源是否被预约
			int isApply = recordList.stream().filter(item ->
					item.getWid() == workdayDO.getWid()
							&& sdf2.format(item.getVisitdate()).equals(params.get("visitdate").toString())
							&& item.getVisitnoon().equals(workdayDO.getAmpm())
							&& item.getSerialnumber() == serialnumber
							&&item.getState().equals("成功")).collect(Collectors.toList()).size();
			if(isApply>0){
				nsNums[i] = 1;//当前号源被预约
			}else{
				nsNums[i] = 0;
			}
		}
		model.addAttribute("nsNums",nsNums);


		String[] nsNumsTime = new String[workdayDO.getNsnum()];
		int size=240/nsNums.length;
		Calendar calendar=Calendar.getInstance();
		calendar.set(Calendar.MINUTE,30);
            /*上午8.30-12.30 4*60=240
            下午1.30-5.30 4*/
		if("上午".equals(workdayDO.getAmpm())){
			calendar.set(Calendar.HOUR_OF_DAY,8);
		}else {
			calendar.set(Calendar.HOUR_OF_DAY,13);
		}
		for (int i=0;i<nsNums.length;i++){
			String time=calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE);
			nsNumsTime[i] = time;
			calendar.add(Calendar.MINUTE,size);
		}
		model.addAttribute("nsNumsTime",nsNumsTime);


		return "yyyh/recordAdd";
	}

	@GetMapping("/edit/{rid}")
	String edit(@PathVariable("rid") Integer rid,Model model){
		RecordDO record = recordService.get(rid);
		model.addAttribute("record", record);
	    return "system/record/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( RecordDO record){
		if(recordService.save(record)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( RecordDO record){
		recordService.update(record);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer rid){
		if(recordService.remove(rid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] rids){
		recordService.batchRemove(rids);
		return R.ok();
	}
	
}
